package models

import (
	"time"

	"gorm.io/gorm"
)

// Streamer 主播
type Streamer struct {
	StreamerId int `gorm:"primary_key" json:"streamer_id"` // id

	InstitutionId  int       `json:"institution_id"`  // 机构ID
	StreamerName   string    `json:"streamer_name"`   // 主播名称
	StreamerMobile string    `json:"streamer_mobile"` // 主播号码
	CreateTime     time.Time `json:"create_time"`     // 创建时间
	UpdateTime     time.Time `json:"update_time"`     // 更新时间

	StreamerTag []StreamerTag
}

// TableName 表名称
func (*Streamer) TableName() string {
	return "streamer"
}

// GetStreamer 查询多条数据
func GetStreamer(fields []string, maps map[string]interface{}) ([]*Streamer, error) {
	var address []*Streamer
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&address).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return address, nil
}

func GetStreamerPage(fields []string, maps map[string]interface{}, pageNum int, pageSize int, order string) ([]*Streamer, error) {
	var Streamer []*Streamer
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Preload("StreamerTag.Tag").Where(cond, vals...).Offset(pageNum).Limit(pageSize).Order(order).Find(&Streamer).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return Streamer, nil
}

// GetStreamerInfo 查询多条数据
func GetStreamerInfo(fields []string, maps map[string]interface{}) (*Streamer, error) {
	var Streamer Streamer
	cond, vals, errs := WhereBuild(maps)
	if errs != nil {
		return nil, errs
	}
	err := db.Select(fields).Where(cond, vals...).Take(&Streamer).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return &Streamer, nil
}

// AddStreamer 插入数据
func AddStreamerDB(data map[string]interface{}, db *gorm.DB) (int, error) {
	address := Streamer{
		StreamerName:   data["streamer_name"].(string),
		StreamerMobile: data["streamer_mobile"].(string),
		InstitutionId:  data["institution_id"].(int),
	}
	result := db.Create(&address)
	if err := result.Error; err != nil {
		return 0, err
	}
	return address.StreamerId, nil
}

// EditStreamer modify a single Streamer
func EditStreamer(where map[string]interface{}, data interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Model(&Streamer{}).Where(cond, vals...).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

// EditStreamer modify a single Streamer
func EditStreamerDB(where map[string]interface{}, data interface{}, db *gorm.DB) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Model(&Streamer{}).Where(cond, vals...).Updates(data).Error; err != nil {
		return err
	}
	return nil
}

func GetStreamerTotal(where map[string]interface{}) (int64, error) {
	var count int64
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return 0, errs
	}
	if err := db.Model(&Streamer{}).Where(cond, vals...).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func CleanStreamer(where map[string]interface{}) error {
	cond, vals, errs := WhereBuild(where)
	if errs != nil {
		return errs
	}
	if err := db.Unscoped().Where(cond, vals...).Delete(&Streamer{}).Error; err != nil {
		return err
	}

	return nil
}
